package ar.edu.unlu.sistemas.movirdroid.business.util;

import java.io.IOException;
import java.io.StringReader;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;

public class XMLManager {
	
	private static XMLManager INSTANCE = new XMLManager();
	private SAXBuilder builder;
	private XMLOutputter outputter;
	
	private XMLManager() {
		this.builder = new SAXBuilder();
		this.outputter = new XMLOutputter();
		
	}
	public static XMLManager getInstance() {
		if (INSTANCE != null) {
			INSTANCE = new XMLManager();
		}
		return INSTANCE;
	}
	
	public SAXBuilder getBuilder() {
		return builder;
	}
	public void setBuilder(SAXBuilder builder) {
		this.builder = builder;
	}
	public XMLOutputter getOutputter() {
		return outputter;
	}
	public void setOutputter(XMLOutputter outputter) {
		this.outputter = outputter;
	}
	
	public Document buildXMLDocFromString(String content) {
		StringReader sr = new StringReader(content);
		try {
			return this.getBuilder().build(sr);
		} catch (JDOMException jdome) {
			Logger.logError(this.getClass().getName(), "XML mal formado", jdome);
		} catch (IOException ioe) {
			Logger.logError(this.getClass().getName(), "ERROR de I/O", ioe);
		}
		return null;
	}
	
	public String xmlDocToString(Document xmlDoc) {
		return this.getOutputter().outputString(xmlDoc);
	}
	
}
